﻿using Application.AppService.CMKAppService;
using Application.AppService.EMRAppService;
using Application.AppService.SSAppServoice;
using Application.AppService.SSDAppServoice;
using AutoMapper;
using Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace UI.Controllers.Pay_Scale_Management_薪酬标准管理_
{
    public class RegisterController : Controller
    {
        private readonly ISSAppService ssAppServoice;
        private readonly ISSDAppServoice sSDAppServoice;
        private readonly IMemoryCache memCache;
        private readonly IMapper mapper;

        public RegisterController(ISSAppService ssAppServoice, IMapper mapper,ISSDAppServoice sSDAppServoice, IMemoryCache memCache)
        {
            this.ssAppServoice = ssAppServoice;
            this.sSDAppServoice = sSDAppServoice;
            this.memCache = memCache;
            this.mapper = mapper;
        }
        /// <summary>
        /// 登记  操作SS表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("AddSS")]
        public async Task<IActionResult> AddSS(SSRegisterDto sSRegisterDto)
        {
            SS ss = mapper.Map<SS>(sSRegisterDto);
            bool rs = await ssAppServoice.AddSSAsync(ss);
            return Json(rs);
        }

        [HttpPost("SelectSSDStandardId")]
        public async Task<IActionResult> SelectSSDStandardId(string StandardId)
        {
            List<SSD> ssd = await sSDAppServoice.SelectSSDStandardIdAsync(StandardId);
            return Json(ssd);
        }

        [HttpPost("AddSSD")]
        public async Task<IActionResult> AddSSD(SSD ssd)
        {
            bool rs = await sSDAppServoice.AddSSDAsync(ssd);
            return Json(rs);
        }

        [HttpPost("UpdateSSD")]
        public async Task<IActionResult> UpdateSSD(SSD ssd)
        {
            bool rs = await sSDAppServoice.UpdateSSDAsync(ssd);
            return Json(rs);
        }
        [HttpPost("GetSSD")]
        public async Task<IActionResult> GetSSD()
        {
            const string cacheId = "SSD";

            if (!memCache.TryGetValue(cacheId, out List<SSD> ssd))
            {
                ssd = await sSDAppServoice.SelectSSDAsync();
                memCache.Set(cacheId, ssd, TimeSpan.FromSeconds(5));
            }
            else
            {
                Console.WriteLine("从缓存获取");
            }

            return Json(ssd);
        }
    }
}
